package com.example.liumuyao.adcproject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cbtService.AndroidSDK.ADCService;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    Handler mHandler;
    float i;
    private final static int pro_msgtype_1=101;
    private final static int MSG_FINISH=1;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ADCService._init();
        createHandler();
        button=(Button)this.findViewById(R.id.button);
        textView= (TextView)this.findViewById(R.id.textView);
        button.setOnClickListener(myOnClickListener);
    }

    View.OnClickListener myOnClickListener=new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(v==button){
                mHandler.post(ui_runnable);
            }
        }
    };
    Runnable ui_runnable=new Runnable() {
        @Override
        public void run() {
            Message msg=new Message();
            msg.what=pro_msgtype_1;
            msg.arg1=MSG_FINISH;
            msg.obj=ADCService._adc_on();
            mHandler.sendMessage(msg);
        }
    };

    private void createHandler(){
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what==pro_msgtype_1){
                    switch (msg.arg1){
                        case MSG_FINISH:
                            i= (float) (((3.3* ADCService._adc_on())/4096));

                            textView.setText("ADC: "+i+"V");
                            mHandler.postDelayed(ui_runnable,1000);
                            break;
                        default:
                            break;

                    }
                }
            }
        };
    }
}


